<template>
  <div class="aiteach">
    <div class="header">
      <h1>AI 教学管理系统</h1>
      <div class="user-info">
        <el-dropdown>
          <span class="el-dropdown-link">
            {{ username }}<el-icon class="el-icon--right"><arrow-down /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <div class="content">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-card class="feature-card" @click="goToPPTGenerator">
            <div class="card-content">
              <el-icon><Document /></el-icon>
              <h3>PPT 生成</h3>
              <p>快速生成教学课件</p>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="feature-card" @click="showQuestionGenerator">
            <div class="card-content">
              <el-icon><QuestionFilled /></el-icon>
              <h3>试题生成</h3>
              <p>智能生成练习题</p>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="feature-card" @click="goToVideoGenerator">
            <div class="card-content">
              <el-icon><VideoPlay /></el-icon>
              <h3>视频生成</h3>
              <p>自动生成教学视频</p>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 试题生成对话框 -->
    <el-dialog
      v-model="questionDialogVisible"
      title="试题生成"
      width="50%"
      :before-close="handleClose"
    >
      <el-form :model="questionForm" label-width="120px">
        <el-form-item label="科目">
          <el-select v-model="questionForm.subject" placeholder="请选择科目">
            <el-option label="数学" value="math" />
            <el-option label="语文" value="chinese" />
            <el-option label="英语" value="english" />
          </el-select>
        </el-form-item>
        <el-form-item label="知识点">
          <el-input v-model="questionForm.knowledge" placeholder="请输入知识点" />
        </el-form-item>
        <el-form-item label="题型">
          <el-checkbox-group v-model="questionForm.types">
            <el-checkbox label="选择题">选择题</el-checkbox>
            <el-checkbox label="填空题">填空题</el-checkbox>
            <el-checkbox label="解答题">解答题</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="难度">
          <el-select v-model="questionForm.difficulty" placeholder="请选择难度">
            <el-option label="简单" value="easy" />
            <el-option label="中等" value="medium" />
            <el-option label="困难" value="hard" />
          </el-select>
        </el-form-item>
        <el-form-item label="题目数量">
          <el-input-number v-model="questionForm.count" :min="1" :max="20" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="questionDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="generateQuestions">生成</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 视频生成对话框 -->
    <el-dialog
      v-model="videoDialogVisible"
      title="视频生成"
      width="50%"
      :before-close="handleClose"
    >
      <el-form :model="videoForm" label-width="120px">
        <el-form-item label="课程主题">
          <el-input v-model="videoForm.topic" placeholder="请输入课程主题" />
        </el-form-item>
        <el-form-item label="课程内容">
          <el-input
            v-model="videoForm.content"
            type="textarea"
            :rows="4"
            placeholder="请输入课程内容"
          />
        </el-form-item>
        <el-form-item label="视频风格">
          <el-select v-model="videoForm.style" placeholder="请选择风格">
            <el-option label="讲解型" value="explanation" />
            <el-option label="演示型" value="demonstration" />
            <el-option label="互动型" value="interactive" />
          </el-select>
        </el-form-item>
        <el-form-item label="视频时长">
          <el-select v-model="videoForm.duration" placeholder="请选择时长">
            <el-option label="5分钟" value="5" />
            <el-option label="10分钟" value="10" />
            <el-option label="15分钟" value="15" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="videoDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="generateVideo">生成</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Document, QuestionFilled, VideoPlay, ArrowDown } from '@element-plus/icons-vue'
import api from '@/api/config'

const router = useRouter()
const username = ref('管理员')

// 对话框显示状态
const questionDialogVisible = ref(false)
const videoDialogVisible = ref(false)

// 试题生成表单
const questionForm = ref({
  subject: '',
  knowledge: '',
  types: [],
  difficulty: '',
  count: 5
})

// 视频生成表单
const videoForm = ref({
  topic: '',
  content: '',
  style: '',
  duration: ''
})

// 获取token
const getToken = async () => {
  try {
    const response = await api.post('/api/user/createApiToken', {}, {
      headers: {
        'Api-Key': import.meta.env.VITE_PPT_API_KEY
      }
    })
    return response.data.token
  } catch (error) {
    console.error('获取token失败:', error)
    ElMessage.error('获取token失败，请重试')
    return null
  }
}

// 跳转到PPT生成页面
const goToPPTGenerator = async () => {
  const token = await getToken()
  if (token) {
    router.push({
      path: '/create-ppt',
      query: { token }
    })
  }
}

// 显示试题生成对话框
const showQuestionGenerator = () => {
  questionDialogVisible.value = true
}

// 跳转到视频生成页面
const goToVideoGenerator = () => {
  router.push('/create-video')
}

// 生成试题
const generateQuestions = async () => {
  try {
    // TODO: 调用试题生成API
    ElMessage.success('试题生成成功！')
    questionDialogVisible.value = false
  } catch (error) {
    ElMessage.error('试题生成失败，请重试')
  }
}

// 生成视频
const generateVideo = async () => {
  try {
    // TODO: 调用视频生成API
    ElMessage.success('视频生成成功！')
    videoDialogVisible.value = false
  } catch (error) {
    ElMessage.error('视频生成失败，请重试')
  }
}

// 关闭对话框
const handleClose = (done: () => void) => {
  done()
}

// 退出登录
const handleLogout = () => {
  router.push('/login')
}
</script>

<style scoped>
.aiteach {
  min-height: 100vh;
  background: #f5f7fa;
}

.header {
  background: white;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.header h1 {
  margin: 0;
  color: #409EFF;
}

.content {
  padding: 40px;
}

.feature-card {
  cursor: pointer;
  transition: all 0.3s;
  height: 200px;
}

.feature-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.card-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
}

.card-content .el-icon {
  font-size: 48px;
  color: #ffffff;
  margin-bottom: 20px;
}

.card-content h3 {
  margin: 10px 0;
  color: #303133;
}

.card-content p {
  margin: 0;
  color: #909399;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 